Dafny - Installation and Formal Verification of Smart Contracts


Slide 1: What is Dafny?


Slide 2: Why Use Dafny for Smart Contracts?


Slide 3: Installing Dafny

  1. Pre-requisites:

    • .NET 6 SDK (for Windows, Linux, macOS).
    • Optionally, Visual Studio Code with Dafny extension for ease of development.
  2. Installation Steps:

    1. Install .NET 6 SDK:

    2. Install Dafny via .NET:

      dotnet tool install -g dafny
      
    3. Verify Installation:

      dafny --version
      
  3. Optional: Install Dafny Extension in Visual Studio Code:

    • Open Visual Studio Code.
    • Go to Extensions (Ctrl+Shift+X) and search for “Dafny”.
    • Install the extension for integrated Dafny support.

Slide 4: Dafny Basics


Slide 5: Formal Verification Using Dafny


Slide 6: Example Smart Contract: Simple Token Transfer


Slide 7: Formalizing the Smart Contract in Dafny


Slide 8: Running Formal Verification in Dafny

  1. Verify the Contract:

    • In Visual Studio Code or from the terminal, run the verification command:
      dafny contract.dfy
      
  2. Check Verification Output:

    • Dafny will either confirm that the contract satisfies all preconditions, postconditions, and invariants, or provide a counterexample if verification fails.
  3. Interpret Dafny’s Output:

    • If any assertions or conditions are violated, Dafny will point out the specific line and provide details about the issue.